package org.openstock.impl.def.core.ui.swing.widget;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.UIManager;

import org.openstock.impl.def.core.factory.Factory;
import org.openstock.impl.def.core.util.SwingUtil;
import org.openstock.resources.image.ImageIconLoader;

import com.jgoodies.looks.plastic.PlasticLookAndFeel;
import com.jgoodies.looks.plastic.theme.SkyKrupp;

public class MainFrame  extends JFrame{
	private static final long serialVersionUID = 1L;

	private static MainFrame instance;
	
	private final JDesktopPane desktop = new JDesktopPane();
	private final MessagePanel messagePanel;
	private final MenuBar menuBar;

	public static MainFrame getInstance(){
		if(instance == null){
			instance = new MainFrame();
		}
		return instance;
	}
	
	public MainFrame() {
		super("Stockz");
		if(instance != null){
			throw new UnsupportedOperationException();
		}
		menuBar = Factory.getSingleton(MenuBar.class);
		messagePanel = Factory.getSingleton(MessagePanel.class);
		instance = this;
	}
	
	public void construct(){
		setLookAndFeel();
		setSize();
		desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
		setJMenuBar(menuBar);
		add(desktop,BorderLayout.CENTER);
		add(messagePanel, BorderLayout.SOUTH);
		setVisible(true);
	}
	
	private void setLookAndFeel(){
		try {
			setIconImage(ImageIconLoader.loadImage("icon.jpg"));
			UIManager.setLookAndFeel("com.jgoodies.looks.plastic.PlasticLookAndFeel");
			PlasticLookAndFeel.setPlasticTheme(new SkyKrupp());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void setSize(){
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Rectangle maxBounds = SwingUtil.getScreenSize();
        setMaximizedBounds(maxBounds);
        Dimension maxDimension = new Dimension(maxBounds.width/3,maxBounds.height/3);
        setMaximumSize(maxDimension);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
		setPreferredSize(maxDimension);
	}

	public MessagePanel getMessagePanel(){
		return messagePanel;
	}
	
}
